class User < ActiveRecord::Base
	validates_presence_of :username, :password, :email
	validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
	
	protected 
	def validate
		puts find_by_username(username)
		errors.add(:username, "Username already taken. Please try a new name") unless find_by_username(username) == nil
	end
	
	has_many :memberships
	
	def groups
		group_ids = memberships.find_by_user_id(:select => [:group])
		groups.find(ids)
	end
end
